package com.sniff.atp.modules.test.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sniff.atp.dto.ContentDTO;
import com.sniff.atp.modules.test.mapper.OpsCustomerOperateMapper;
import com.sniff.atp.modules.test.model.OpsMembershipChange;
import com.sniff.atp.modules.test.model.ops.OpsCustomerOperate;
import com.sniff.atp.modules.test.service.OpsCustomerOperateService;
import com.sniff.atp.utils.DBUtil;
import com.sniff.atp.utils.HttpClientUtil;
import com.sniff.atp.utils.JsonUtil;
import com.sniff.atp.utils.ToolsUtil;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 操作用户信息 服务实现类
 * </p>
 *
 * @author macro
 * @since 2024-01-14
 */
@Service
public class OpsCustomerOperateServiceImpl extends ServiceImpl<OpsCustomerOperateMapper, OpsCustomerOperate> implements OpsCustomerOperateService {

    @Override
    public boolean create(OpsCustomerOperate opsCustomerOperate) {
        opsCustomerOperate.setCreateTime(new Date());
        opsCustomerOperate.setUpdateTime(new Date());
        return save(opsCustomerOperate);
    }

    @Override
    public boolean update(Long id, OpsCustomerOperate opsCustomerOperate) {
        opsCustomerOperate.setOpsCustomerOperateId(id);
        opsCustomerOperate.setUpdateTime(new Date());
        return updateById(opsCustomerOperate);
    }

    /**
     * 分页查询
     *
     * @param opsCustomerOperate
     * @param pageSize
     * @param pageNum
     * @return
     * @param opsCustomerOperate
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public Page<OpsCustomerOperate> list(OpsCustomerOperate opsCustomerOperate, Integer pageSize, Integer pageNum) {
        Page<OpsCustomerOperate> page = new Page<>(pageNum, pageSize);
        QueryWrapper<OpsCustomerOperate> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(StringUtil.isNotBlank(opsCustomerOperate.getCustomerName()), OpsCustomerOperate::getCustomerName,opsCustomerOperate.getCustomerName())
                .like(StringUtil.isNotBlank(opsCustomerOperate.getUnificationCustomerFullName()), OpsCustomerOperate::getUnificationCustomerFullName,opsCustomerOperate.getUnificationCustomerFullName())
                .le(opsCustomerOperate.getSuperCustomerId()!= null,OpsCustomerOperate::getSuperCustomerId,opsCustomerOperate.getSuperCustomerId())
                .ge(opsCustomerOperate.getCreateTime() != null,OpsCustomerOperate::getCreateTime,opsCustomerOperate.getStartTime())
                .le(opsCustomerOperate.getCreateTime() != null,OpsCustomerOperate::getCreateTime,opsCustomerOperate.getEndTime())
                .orderByDesc(OpsCustomerOperate::getCreateTime);
        return page(page, wrapper);
    }

    /**
     * @param opsCustomerOperate 删除用户
     * @return
     */
    @Override
    public boolean deleteCustomerBack(OpsCustomerOperate opsCustomerOperate) {
        opsCustomerOperate.setCreateTime(new Date());
        opsCustomerOperate.setUpdateTime(new Date());
        //请求操作信息
        try {
            Map<String,String> map = new HashMap<>();
            map.put("superCustomerId", opsCustomerOperate.getSuperCustomerId().toString());

            //删除用户接口
            String result = new HttpClientUtil().doDelete("https://gateway-prod.theckb.com/customer/backdoor/customer/deleteCustomerBack", map);
            String code = JsonUtil.extractString(result, "code");
            if("0".equals(code)){
                opsCustomerOperate.setStatus("成功");
                opsCustomerOperate.setCustomerStatus(2);
            }else{
                opsCustomerOperate.setStatus("失败");
            }

        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return save(opsCustomerOperate);
    }

    /**
     * @param opsCustomerOperate 增加内部用户标记
     * @return
     */
    @Override
    public boolean internalCustomerBack(OpsCustomerOperate opsCustomerOperate) {
        opsCustomerOperate.setCreateTime(new Date());
        opsCustomerOperate.setUpdateTime(new Date());
        //请求操作信息
        try {
            Map<String, String> params = new HashMap<>();
            params.put("superCustomerId", opsCustomerOperate.getSuperCustomerId().toString());

            //删除用户接口
            String result = new HttpClientUtil().doPostNeedTokenPamrm("https://gateway-prod.theckb.com/customer/backdoor/customer/internalCustomerBack", params);
            String code = JsonUtil.extractString(result, "code");
            if("0".equals(code)){
                opsCustomerOperate.setStatus("成功");
                opsCustomerOperate.setInternalFlag(1);
            }else{
                opsCustomerOperate.setStatus("失败");
            }

        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return save(opsCustomerOperate);
    }

    /**
     * 获取用户基本信息
     */
    public OpsCustomerOperate qryCustomerInfoByCustomerId(OpsCustomerOperate opsCustomerOperate) {
        List<JSONObject> select = DBUtil.select("prod-customer-查询用户基本信息", new ContentDTO("customerId", opsCustomerOperate.getCustomerId().toString()));
        for (JSONObject o: select) {
            String customerId = o.getString("customer_id");
            String superCustomerId = o.getString("super_customer_id");
            String unificationCustomerFullName = o.getString("unification_customer_full_name");
            String customerName = o.getString("customer_name");
            String loginName = o.getString("login_name");

            opsCustomerOperate.setCustomerId(Long.valueOf(customerId));
            opsCustomerOperate.setSuperCustomerId(Long.valueOf(superCustomerId));
            opsCustomerOperate.setCustomerName(unificationCustomerFullName);
            opsCustomerOperate.setCustomerName(customerName);
            opsCustomerOperate.setLoginName(loginName);
        }
        return opsCustomerOperate;
    }


}